package domain

import (
	"AcsGo/internal/util"
	"fmt"
)

type BookBorrowBookRequest struct {
	Code             util.Field
	Fill             util.Field
	TransactionDate  util.Field
	NbDueDate        util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	ItemIdentifier   util.FieldPrefix
	TerminalPassword util.FieldPrefix
	Operator         util.FieldPrefix
}

func HandleBorrowBookRequest(str string) (*BookBorrowBookRequest, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	var query = BookBorrowBookRequest{
		Code:             util.Field{Value: "11", Length: 2},
		Fill:             util.Field{Value: "YN", Length: 2},
		TransactionDate:  util.Field{Value: "", Length: 18},
		NbDueDate:        util.Field{Value: "", Length: 18},
		InstitutionId:    util.FieldPrefix{Value: "", Prefix: "AO", Suffix: util.PrefixLink},
		PatronIdentifier: util.FieldPrefix{Value: "", Prefix: "AA", Suffix: util.PrefixLink},
		ItemIdentifier:   util.FieldPrefix{Value: "", Prefix: "AB", Suffix: util.PrefixLink},
		TerminalPassword: util.FieldPrefix{Value: "", Prefix: "AC", Suffix: util.PrefixLink},
		Operator:         util.FieldPrefix{Value: "JHJ", Prefix: "CN", Suffix: util.PrefixLink},
	}

	length := query.Code.Length + query.Fill.Length + query.TransactionDate.Length + query.NbDueDate.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.Fill.GetValue(strTemp)
	strTemp = query.TransactionDate.GetValue(strTemp)
	strTemp = query.NbDueDate.GetValue(strTemp)
	strTemp = query.InstitutionId.GetValue(strTemp)
	strTemp = query.PatronIdentifier.GetValue(strTemp)
	strTemp = query.ItemIdentifier.GetValue(strTemp)
	strTemp = query.TerminalPassword.GetValue(strTemp)
	_ = query.Operator.GetValue(strTemp)

	return &query, nil
}

type BookBorrowBookResponse struct {
	Code             util.Field
	Ok               util.Field
	Fill             util.Field
	TransactionDate  util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	PersonalName     util.FieldPrefix
	ItemIdentifier   util.FieldPrefix
	TitleIdentifier  util.FieldPrefix
	DueDate          util.FieldPrefix
	ItemProperties   util.FieldPrefix
	ScreenMessage    util.FieldPrefix
	PrintLine        util.FieldPrefix
	SequenceNumber   util.FieldPrefix
	ForbidRdtype     util.FieldPrefix
}

func (q BookBorrowBookResponse) StringFormat() (string, error) {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		return "", err
	}
	return sb, nil
}

func InitBorrowBookResponse(success, rdbm, rdname, bkbm, bkTitle, dueDate, price string) *BookBorrowBookResponse {
	var queryResponse = BookBorrowBookResponse{
		Code:             util.Field{Value: "12", Length: 2},
		Ok:               util.Field{Value: "1", Length: 1},
		Fill:             util.Field{Value: "NNY", Length: 2},
		TransactionDate:  util.Field{Value: util.GetCurrentTimeInFormat(), Length: 18},
		InstitutionId:    util.FieldPrefix{Value: "", Prefix: "AO", Suffix: util.PrefixLink},
		PatronIdentifier: util.FieldPrefix{Value: "", Prefix: "AA", Suffix: util.PrefixLink},
		PersonalName:     util.FieldPrefix{Value: "", Prefix: "AE", Suffix: util.PrefixLink},
		ItemIdentifier:   util.FieldPrefix{Value: "", Prefix: "AB", Suffix: util.PrefixLink},
		TitleIdentifier:  util.FieldPrefix{Value: "", Prefix: "AJ", Suffix: util.PrefixLink},
		DueDate:          util.FieldPrefix{Value: "", Prefix: "AH", Suffix: util.PrefixLink},
		ItemProperties:   util.FieldPrefix{Value: "", Prefix: "CH", Suffix: util.PrefixLink},
		ScreenMessage:    util.FieldPrefix{Value: "", Prefix: "AF", Suffix: util.PrefixLink},
		SequenceNumber:   util.FieldPrefix{Value: "3AZD2A1", Prefix: "AY", Suffix: util.PrefixLink},
		ForbidRdtype:     util.FieldPrefix{Value: "", Prefix: "BL", Suffix: util.PrefixLink},
	}

	queryResponse.Ok.Value = success
	queryResponse.PatronIdentifier.Value = rdbm
	queryResponse.PersonalName.Value = rdname
	queryResponse.ItemIdentifier.Value = bkbm
	queryResponse.TitleIdentifier.Value = bkTitle
	queryResponse.DueDate.Value = dueDate
	queryResponse.ItemProperties.Value = fmt.Sprintf("ATY%s", price)

	switch success {
	case "1":
		queryResponse.ScreenMessage.Value = fmt.Sprintf("图书[%s]借书处理成功！ 应还日期:%s", bkbm, dueDate)
	case "2":
		// 处理 success 为 "2" 的情况
		// 如果没有特别的操作，这里可以留空或者添加特定的逻辑
		// queryResponse.ScreenMessage.Value = "特定处理"
		queryResponse.ScreenMessage.Value = fmt.Sprintf("读者[%s]借书超过最大借书量,最大借书量[%s]", rdbm, bkTitle)

	default:
		queryResponse.ScreenMessage.Value = fmt.Sprintf("图书[%s]借书处理失败 请联系管理员", bkbm)
	}

	return &queryResponse
}
